package com.pearadmin.pro.modules.experiment.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pearadmin.pro.common.context.UserContext;
import com.pearadmin.pro.common.web.base.page.PageResponse;
import com.pearadmin.pro.common.web.base.page.Pageable;
import com.pearadmin.pro.modules.experiment.entity.ExperimentInfo;
import com.pearadmin.pro.modules.experiment.entity.ExperimentInfoCount;
import com.pearadmin.pro.modules.experiment.entity.ExperimentInfoRequest;
import com.pearadmin.pro.modules.experiment.mapper.ExperimentInfoDao;
import com.pearadmin.pro.modules.experiment.mapper.ExperimentMajorDao;
import com.pearadmin.pro.modules.experiment.service.ExperimentService;
import com.pearadmin.pro.modules.statistics.vo.StatisticsVO;
import com.pearadmin.pro.modules.sys.domain.SysConfig;
import com.pearadmin.pro.modules.sys.param.SysConfigRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class ExperimentServiceImpl extends ServiceImpl<ExperimentInfoDao, ExperimentInfo> implements ExperimentService {

    @Resource
    private ExperimentInfoDao experimentInfoDao;

    @Resource
    private ExperimentMajorDao experimentMajorDao;

    @Resource
    private UserContext userContext;

    @Override
    public List<ExperimentInfo> homeRecommendation() {
        return  experimentInfoDao.homeRecommendation();
    }

    @Override
    public List<ExperimentInfo> screeningSimulationExperiments(Map<String, String> param) {
        String flag = param.get("flag");
        String id = param.get("id");
        if(flag.equals("0")){
            // 当flag = 0 的时候
            // 说明是父级改变
            if(id.equals("1")){
                return experimentInfoDao.selectAll();
            }
            return  experimentInfoDao.getExperimentByParsent(id);
        }else if(flag.equals("999")){
            return  experimentInfoDao.selectAll();
        }else {
            return  experimentInfoDao.getExperimentByMajor(id);
        }
    }

    @Override
    public ExperimentInfo detailsOfIndividualExperiments(String id) {
        ExperimentInfo experimentInfo = experimentInfoDao.selectByPrimaryKey(Integer.valueOf(id));
        String parentName = experimentMajorDao.selectName(experimentInfo.getMajor());
        experimentInfo.setMajorClassification(parentName);
        return  experimentInfo ;

    }

    @Override
    public PageResponse<ExperimentInfo> page(ExperimentInfoRequest request) {
        PageResponse<ExperimentInfo> of = Pageable.of(request, (() -> experimentInfoDao.selectInfo(request)));
        List<ExperimentInfo> record = of.getRecord();
        record.forEach(experimentInfo -> {
            String parentName = experimentMajorDao.selectName(experimentInfo.getMajor());
            experimentInfo.setMajorClassification(parentName);
        });
        of.setRecord(record);
        return of;
    }

    @Override
    public Boolean editCount(ExperimentInfoCount experimentInfoCount) {
        Integer aBoolean = experimentInfoDao.editCount(experimentInfoCount);
        if(aBoolean>0){
            experimentInfoDao.insertCount(experimentInfoCount);
        }
        return null;
    }

    @Override
    public int statistics() {
        return experimentInfoDao.statistics();
    }

    @Override
    public List<StatisticsVO> statisticsSeparate() {
        return experimentInfoDao.statisticsSeparate();
    }

    @Override
    public List<StatisticsVO> statisticsVOS_side() {
        return experimentInfoDao.statisticsVOS_side();
    }

}
